package com.tianhui.consignortwofei.mvp.ui.activity.display;

import android.text.TextUtils;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.DateUtil;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.interf.IChartViewInfo;
import com.tianhui.consignortwofei.interf.OnDisplayDataShowListener;
import com.tianhui.consignortwofei.mvp.model.enty.ChartViewInfo;
import com.tianhui.consignortwofei.mvp.model.enty.statistics.DriverCarStatisticalInfo;
import com.tianhui.consignortwofei.mvp.model.enty.statistics.OrderStatisticalInfo;
import com.tianhui.consignortwofei.mvp.model.enty.statistics.StatisticsDataInfo;
import com.tianhui.consignortwofei.mvp.ui.fragment.display.BarDisplayDataFragment;
import com.tianhui.consignortwofei.mvp.ui.fragment.display.LineDisplayDataFragment;
import com.tianhui.consignortwofei.widget.StatisticsDataView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * @author fgs
 * @date 2021/6/15
 * @description
 **/
public class DisplayDataActivity extends BaseDisplayDataActivity {

    @BindView(R.id.activity_display_data_statisticsDataView)
    StatisticsDataView statisticsDataView;

    private StatisticsDataInfo statisticsDataInfo = new StatisticsDataInfo();

    private boolean isBarView = true;

    private FragmentManager fragmentManager;
    private CommonFragment currentFragment;
    private BarDisplayDataFragment barDisplayDataFragment;
    private LineDisplayDataFragment lineDisplayDataFragment;
    private OnDisplayDataShowListener onDisplayDataShowListener;
    List<IChartViewInfo> orderStatisticsInfoList;
    List<IChartViewInfo> freightStatisticsInfoList;
    List<IChartViewInfo> driverStatisticsInfoList;
    List<IChartViewInfo> carStatisticsInfoList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_display_data;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public String getToolbarSubtitle() {
        return "查看折线图";
    }

    @Override
    public void onSubtitleClick() {
        if (isBarView) {
            setSubTitle("查看柱状图");
            onDisplayDataShowListener = lineDisplayDataFragment;
            switchDisplayView(lineDisplayDataFragment);
        } else {
            setSubTitle("查看折线图");
            onDisplayDataShowListener = barDisplayDataFragment;
            switchDisplayView(barDisplayDataFragment);
        }
        isBarView = !isBarView;


    }

    private void switchDisplayView(CommonFragment commonFragment) {

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.hide(currentFragment);
        fragmentTransaction.show(commonFragment);
        fragmentTransaction.commit();

        currentFragment = commonFragment;


        onDisplayDataShowListener.showNewCarDisplayData(carStatisticsInfoList);
        onDisplayDataShowListener.showNewDriverDisplayData(driverStatisticsInfoList);
        onDisplayDataShowListener.showNewOrderDisplayData(orderStatisticsInfoList);
        onDisplayDataShowListener.showNewFreightDisplayData(freightStatisticsInfoList);
    }


    @Override
    void initData() {
        String date = DateUtil.getStandardTime(System.currentTimeMillis(), "yyyy-MM-dd");

        requestOrderStatisticsInfo(date);
        requestDriverCarStatisticsInfo(date);


        barDisplayDataFragment = new BarDisplayDataFragment();
        lineDisplayDataFragment = new LineDisplayDataFragment();

        fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.add(R.id.activity_display_data_containerFrameLayout, barDisplayDataFragment);
        fragmentTransaction.add(R.id.activity_display_data_containerFrameLayout, lineDisplayDataFragment);
        fragmentTransaction.hide(lineDisplayDataFragment);
        fragmentTransaction.commit();
        currentFragment = barDisplayDataFragment;

        onDisplayDataShowListener = barDisplayDataFragment;
    }

    @Override
    void searchData(String startDate, String endDate) {
        requestOrderDisplayData(startDate, endDate);
        requestDriverCarDisplayData(startDate, endDate);
    }


    private void requestOrderDisplayData(String startDate, String endDate) {
        Map<String, String> map = new HashMap<>();
        map.put("begindate", startDate);
        map.put("enddate", endDate);
        map.put("belongarea", "2");
        statisticsModel.orderStatisticsDataModel(getContext(), map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<OrderStatisticalInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<OrderStatisticalInfo>> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    List<OrderStatisticalInfo> list = commonResponse.getData();
                    if (list != null && list.size() > 0) {
                        processOrderDisplayData(list);
                    }

                } else {
                    showToast(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }

    private void requestDriverCarDisplayData(String startDate, String endDate) {
        Map<String, String> map = new HashMap<>();
        map.put("begindate", startDate);
        map.put("enddate", endDate);
        statisticsModel.driverCarStatisticsDataModel(getContext(), map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<DriverCarStatisticalInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<DriverCarStatisticalInfo>> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    List<DriverCarStatisticalInfo> list = commonResponse.getData();
                    if (list != null && list.size() > 0) {
                        processDriverCarDisplayData(list);
                    }

                } else {
                    showToast(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }

    private void processOrderDisplayData(List<OrderStatisticalInfo> list) {
        orderStatisticsInfoList = new ArrayList<>();
        freightStatisticsInfoList = new ArrayList<>();
        ChartViewInfo orderResultInfo;
        ChartViewInfo freightResultInfo;
        OrderStatisticalInfo info = null;
        int resultSize = list.size();
        for (int i = 0; i < resultSize; i++) {
            info = list.get(i);
//            if (!TextUtils.isEmpty(info.belongarea) && info.belongarea.equals("2")) {
            orderResultInfo = new ChartViewInfo();
            orderResultInfo.value = info.total;
            orderResultInfo.label = info.createtime;
            orderStatisticsInfoList.add(orderResultInfo);

            freightResultInfo = new ChartViewInfo();
            if (!TextUtils.isEmpty(info.amountTotal)) {
                freightResultInfo.value = new BigDecimal(info.amountTotal).divide(new BigDecimal("1000000"), 5, BigDecimal.ROUND_HALF_UP).doubleValue() + "";
            } else {
                freightResultInfo.value = "0";
            }
            freightResultInfo.label = info.createtime;
            freightStatisticsInfoList.add(freightResultInfo);
//            }

        }
        if (onDisplayDataShowListener != null) {
            onDisplayDataShowListener.showNewOrderDisplayData(orderStatisticsInfoList);
            onDisplayDataShowListener.showNewFreightDisplayData(freightStatisticsInfoList);
        }
    }

    private void processDriverCarDisplayData(List<DriverCarStatisticalInfo> list) {

        driverStatisticsInfoList = new ArrayList<>();
        carStatisticsInfoList = new ArrayList<>();
        ChartViewInfo driverResultInfo;
        ChartViewInfo carResultInfo;
        DriverCarStatisticalInfo info = null;
        int resultSize = list.size();
        for (int i = 0; i < resultSize; i++) {
            info = list.get(i);
            driverResultInfo = new ChartViewInfo();
            driverResultInfo.value = info.yesdrivernow;
            driverResultInfo.label = info.createtime;
            driverStatisticsInfoList.add(driverResultInfo);


            carResultInfo = new ChartViewInfo();
            carResultInfo.value = info.yesdrivercarnow;
            carResultInfo.label = info.createtime;
            carStatisticsInfoList.add(carResultInfo);

        }
        if (onDisplayDataShowListener != null) {
            onDisplayDataShowListener.showNewDriverDisplayData(driverStatisticsInfoList);
            onDisplayDataShowListener.showNewCarDisplayData(carStatisticsInfoList);
        }

    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    private void requestOrderStatisticsInfo(String date) {
        Map<String, String> map = new HashMap<>();
        map.put("dates", date);
        statisticsModel.orderStatisticsInfoModel(this, map, false, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<OrderStatisticalInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<OrderStatisticalInfo>> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    List<OrderStatisticalInfo> list = commonResponse.getData();
                    if (list != null && list.size() > 0) {
                        processOrderStatisticsInfo(list);
                    }

                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });

    }

    private void processOrderStatisticsInfo(List<OrderStatisticalInfo> list) {

        List<IChartViewInfo> orderStatisticsInfoList = new ArrayList<>();
        ChartViewInfo resultInfo;
        OrderStatisticalInfo info;
        OrderStatisticalInfo TJInfo = null;
        OrderStatisticalInfo HBInfo = null;
        int resultSize = list.size();
        for (int i = 0; i < resultSize; i++) {
            resultInfo = new ChartViewInfo();
            int size = list.size();
            if (list != null && size > 0) {
                for (int j = 0; j < size; j++) {
                    info = list.get(j);
                    if (!TextUtils.isEmpty(info.belongarea) && info.belongarea.equals("1")) {
                        HBInfo = info;
                    } else if (!TextUtils.isEmpty(info.belongarea) && info.belongarea.equals("2")) {
                        TJInfo = info;
                    }

                }
                if (TJInfo != null && HBInfo != null) {
                    resultInfo.value = new BigDecimal(TJInfo.totalNow).add(new BigDecimal(HBInfo.totalNow)).toString();
                    orderStatisticsInfoList.add(resultInfo);

                }
            }
        }
        if (TJInfo != null && HBInfo != null) {
            String totalOrderValue;
            totalOrderValue = new BigDecimal(TJInfo.total).add(new BigDecimal(HBInfo.total)).toString();

            String totalAmountValue;
            totalAmountValue = new BigDecimal(TJInfo.amountTotal).add(new BigDecimal(HBInfo.amountTotal)).toString();

            statisticsDataInfo.allOrder = totalOrderValue;
            statisticsDataInfo.allAmount = totalAmountValue;
            statisticsDataView.setStatisticsViewData(statisticsDataInfo);
        }
    }

    private void requestDriverCarStatisticsInfo(String date) {
        Map<String, String> map = new HashMap<>();
        map.put("dates", date);
        statisticsModel.driverCarStatisticsInfoModel(this, map, false, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<DriverCarStatisticalInfo>>() {
            @Override
            public void onNext(CommonResponse<DriverCarStatisticalInfo> commonResponse) {
                if (commonResponse.getCode() == 20000) {

                    DriverCarStatisticalInfo info = commonResponse.getData();
                    if (info != null) {
                        processDriverCarStatisticsInfo(info);
                    }
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });

    }

    private void processDriverCarStatisticsInfo(DriverCarStatisticalInfo info) {
        if (info != null) {
            String totalDriver = new BigDecimal(info.drivertotal).add(new BigDecimal(info.yesdrivertotal)).toString();
            String totalCar = new BigDecimal(info.drivercartotal).add(new BigDecimal(info.yesdrivercartotal)).toString();
            statisticsDataInfo.allDriver = totalDriver;
            statisticsDataInfo.allCar = totalCar;
            statisticsDataView.setStatisticsViewData(statisticsDataInfo);
        }


    }
}
